package com.yuke.cloud.service.cmc.service.impl;

import com.yuke.cloud.common.core.utils.RequestUtil;
import com.yuke.cloud.service.api.opc.dto.UploadFile;
import com.yuke.cloud.service.api.opc.service.OpcFileFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsAppBackgroundMapper;
import com.yuke.cloud.service.cmc.dto.CmsAppBackgroundDto;
import com.yuke.cloud.service.cmc.dto.MallCmsAppBackground;
import com.yuke.cloud.service.cmc.entity.CmsAppBackground;
import com.yuke.cloud.service.cmc.entity.CmsAppBackgroundPage;
import com.yuke.cloud.service.cmc.service.CmsAppBackgroundPageService;
import com.yuke.cloud.service.cmc.service.CmsAppBackgroundService;
import com.yuke.cloud.common.core.support.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by wg on 2019/01/18.
 */
@Slf4j
@Service
@Transactional
public class CmsAppBackgroundServiceImpl extends BaseService<CmsAppBackground> implements CmsAppBackgroundService {
    @Resource
    private CmsAppBackgroundMapper cmsAppBackgroundMapper;
    @Resource
    private CmsAppBackgroundPageService cmsAppBackgroundPageService;
    @Resource
    private OpcFileFeignApi opcFileFeignApi;

    @Override
    public int save(CmsAppBackground record) {
        record.setUpdateUser(RequestUtil.getLoginUser().getUserId());
        return super.save(record);
    }

    @Override
    public int update(CmsAppBackground entity) {
        entity.setUpdateUser(RequestUtil.getLoginUser().getUserId());
        int count = super.update(entity);
        if (count > 0 && entity.getOldPicUrl() != null && !"".equals(entity.getOldPicUrl())) {
            List<UploadFile> uploadFiles = new ArrayList<>();
            UploadFile uploadFile = new UploadFile();
            uploadFile.setFilePath(entity.getOldPicUrl());
            uploadFiles.add(uploadFile);
            opcFileFeignApi.fileDelete(uploadFiles);
        }
        return count;
    }

    @Override
    public int saveOrUpdateBackground(CmsAppBackgroundDto cmsAppBackgroundDto) {
        if (cmsAppBackgroundDto == null)
            return 0;
        CmsAppBackground cmsAppBackground = cmsAppBackgroundDto.getCmsAppBackground();
        int count = 0;
        if (cmsAppBackground != null) {
            if (cmsAppBackground.getBackgroundId() == null) {
                //save
                count = this.save(cmsAppBackground);
                cmsAppBackgroundPageService.usePage(cmsAppBackground, cmsAppBackgroundDto.getPageIds());
            } else {
                //update
                count = this.update(cmsAppBackground);
                cmsAppBackgroundPageService.usePage(cmsAppBackground, cmsAppBackgroundDto.getPageIds());
            }
        }
        return count;
    }

    @Override
    public CmsAppBackgroundDto getByBackgroundType(Byte backgroundType) {
        CmsAppBackgroundDto cmsAppBackgroundDto = new CmsAppBackgroundDto();
        CmsAppBackground cmsAppBackground = new CmsAppBackground();
        cmsAppBackground.setBackgroundType(backgroundType);
        cmsAppBackgroundDto.setCmsAppBackground(cmsAppBackgroundMapper.selectOne(cmsAppBackground));
        if (cmsAppBackgroundDto.getCmsAppBackground() != null) {
            CmsAppBackgroundPage cmsAppBackgroundPage = new CmsAppBackgroundPage();
            cmsAppBackgroundPage.setBackgroundId(cmsAppBackgroundDto.getCmsAppBackground().getBackgroundId());
            List<CmsAppBackgroundPage> cmsAppBackgroundPageList = cmsAppBackgroundPageService.select(cmsAppBackgroundPage);
            if (cmsAppBackgroundPageList != null && cmsAppBackgroundPageList.size() > 0) {
                List<Long> pageIds = cmsAppBackgroundPageList.stream().map(CmsAppBackgroundPage::getPageId).collect(Collectors.toList());
                cmsAppBackgroundDto.setPageIds(pageIds);
            }
        }
        return cmsAppBackgroundDto;
    }

    @Override
    public List<MallCmsAppBackground> getListDetailByCondition(MallCmsAppBackground mallCmsAppBackground) {
//        log.info("--- Enter getListDetailByCondition(MallCmsAppBackground mallCmsAppBackground)，参数：{} ---", mallCmsAppBackground);
        List<MallCmsAppBackground> mallCmsAppBackgrounds = cmsAppBackgroundMapper.getListDetailByCondition(mallCmsAppBackground);
//        log.info("--- Leave getListDetailByCondition(MallCmsAppBackground mallCmsAppBackground)，返回值：{} ---", mallCmsAppBackgrounds);
        return mallCmsAppBackgrounds;
    }
}
